//
//  DLPhotoCameraUtil.m
//  DGGPhotoCameraSDK
//
//  Created by 伍明鹏 on 2019/12/31.
//

#import "DLPhotoCameraUtil.h"
#import "DLPhotoBrowser.h"

@interface DLPhotoCameraUtil()



@end

@implementation DLPhotoCameraUtil

- (instancetype)init{
    if (self = [super init]) {
        [self actionSheet];
    }
    return self;
}

- (void)showPhotoAlbumWithCurrentController:(UIViewController *)currentController
                                    preview:(BOOL)preview
                           choosePhotoBlock:(ChoosePhotoBlock)choosePhotoBlock
                      choosePhotoErrorBlock:(ChoosePhotoErrorBlock)choosePhotoErrorBlock{
    
#pragma mark - 参数配置 optional，可直接使用 defaultPhotoConfiguration
    //以下参数为自定义参数，均可不设置，有默认值
    self.actionSheet.configuration.sortAscending = YES;
    self.actionSheet.configuration.allowSelectImage = self.allowSelectImage;
    self.actionSheet.configuration.allowSelectGif = YES;
    self.actionSheet.configuration.allowSelectVideo = self.allowSelectVideo;
    self.actionSheet.configuration.allowSelectLivePhoto = YES;
    self.actionSheet.configuration.allowForceTouch = YES;
    self.actionSheet.configuration.allowEditImage = self.allowEditImage;
    self.actionSheet.configuration.allowEditVideo = YES;
    self.actionSheet.configuration.allowSlideSelect = YES;
    self.actionSheet.configuration.allowMixSelect = YES;
    self.actionSheet.configuration.allowDragSelect = NO;
    //设置相册内部显示拍照按钮
    self.actionSheet.configuration.allowTakePhotoInLibrary = self.allowTakePhotoInLibrary;
//    //设置在内部拍照按钮上实时显示相机俘获画面
    self.actionSheet.configuration.showCaptureImageOnTakePhotoBtn = YES;
    //设置照片最大预览数
    self.actionSheet.configuration.maxPreviewCount = 20;
    //设置照片最大选择数
    self.actionSheet.configuration.maxSelectCount = self.maxSelectCount;
    self.actionSheet.configuration.maxVideoSelectCountInMix = self.maxVideoSelectCountInMix;
    self.actionSheet.configuration.minVideoSelectCountInMix = self.minVideoSelectCountInMix;
    self.actionSheet.configuration.allowRecordVideo = self.allowRecordVideo;
    //设置允许选择的视频最大时长
    self.actionSheet.configuration.maxVideoDuration = 120;
    //设置照片cell弧度
    self.actionSheet.configuration.cellCornerRadio = 0;
    //单选模式是否显示选择按钮
        self.actionSheet.configuration.showSelectBtn = YES;
    //是否在选择图片后直接进入编辑界面
    self.actionSheet.configuration.editAfterSelectThumbnailImage = NO;
    self.actionSheet.configuration.showSelectedIndex = self.showSelectedIndex;
    //是否保存编辑后的图片
    self.actionSheet.configuration.saveNewImageAfterEdit = self.saveNewImageAfterEdit;
    //设置编辑比例
    self.actionSheet.configuration.clipRatios = @[GetClipRatio(1, 1)];
    //是否在已选择照片上显示遮罩层
    self.actionSheet.configuration.showSelectedMask = YES;
    self.actionSheet.configuration.selectedMaskColor = [UIColor colorWithWhite:255/255.0 alpha:0.3];
    self.actionSheet.configuration.navBarColor = self.navBarColor;
    self.actionSheet.configuration.navTitleColor = self.tintColor;
    self.actionSheet.configuration.bottomBtnsNormalTitleColor = self.bottomBtnsNormalTitleColor;
    self.actionSheet.configuration.bottomBtnsNormalDoneTitleColor = self.bottomBtnsNormalDoneTitleColor;
    self.actionSheet.configuration.bottomViewBgColor = self.navBarColor;
    self.actionSheet.configuration.bottomBtnsNormalBgColor = self.bottomBtnsNormalBgColor;
    self.actionSheet.configuration.bottomBtnsDisableBgColor = self.bottomBtnsDisableBgColor;
    self.actionSheet.configuration.indexLabelBgColor = self.indexLabelBgColor;
    self.actionSheet.configuration.cameraProgressColor = self.cameraProgressColor;
    self.actionSheet.configuration.allowSelectOriginal = YES;
    self.actionSheet.configuration.statusBarStyle = UIStatusBarStyleDefault;
    self.actionSheet.configuration.maxRecordDuration = self.maxRecordDuration;
    
    //是否允许框架解析图片
    self.actionSheet.configuration.shouldAnialysisAsset = self.shouldAnialysisAsset;
    //框架语言
    self.actionSheet.configuration.languageType = DLLanguageSystem;
    //是否使用系统相机
    self.actionSheet.configuration.useSystemCamera = self.useSystemCamera;
#pragma mark - required
    //如果调用的方法没有传sender，则该属性必须提前赋值
    self.actionSheet.sender = currentController;
    [self.actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        
        if (choosePhotoBlock) {
            choosePhotoBlock(images,assets,isOriginal);
        }
        NSLog(@"image:%@", images);
        //解析图片
    }];
    self.actionSheet.selectImageRequestErrorBlock = ^(NSArray<PHAsset *> * _Nonnull errorAssets, NSArray<NSNumber *> * _Nonnull errorIndex) {
        if (choosePhotoErrorBlock) {
            choosePhotoErrorBlock(errorAssets,errorIndex);
        }
        NSLog(@"图片解析出错的索引为: %@, 对应assets为: %@", errorIndex, errorAssets);
    };
    
    self.actionSheet.cancleBlock = ^{
        NSLog(@"取消选择图片");
    };
    if (preview) {
        [self.actionSheet showPreviewAnimated:YES];
    }else{
        [self.actionSheet showPhotoLibrary];
    }
}
- (void)showCameraWithCurrentController:(UIViewController *)currentController takePhotoBlock:(TakePhotoBlock)takePhotoBlock{
    DLCustomCamera *camera = [[DLCustomCamera alloc] init];
    camera.allowRecordVideo = self.allowRecordVideo;
    camera.allowTakePhoto = self.allowTakePhoto;
    camera.doneBlock = ^(UIImage *image, NSURL *videoUrl) {
        if (takePhotoBlock) {
            takePhotoBlock(image,videoUrl);
        }
    };
    [currentController showDetailViewController:camera sender:nil];
}

#pragma mark - Lazy

- (void)setNavBarColor:(UIColor *)navBarColor{
    _navBarColor = navBarColor;
    self.actionSheet.configuration.navBarColor = navBarColor;
    self.actionSheet.configuration.bottomViewBgColor = navBarColor;
}

- (void)setTintColor:(UIColor *)tintColor{
    _tintColor = tintColor;
    self.actionSheet.configuration.navTitleColor = tintColor;
}

- (void)setBottomBtnsNormalDoneTitleColor:(UIColor *)bottomBtnsNormalDoneTitleColor {
    _bottomBtnsNormalDoneTitleColor = bottomBtnsNormalDoneTitleColor;
    self.actionSheet.configuration.bottomBtnsNormalDoneTitleColor = bottomBtnsNormalDoneTitleColor;
}

- (void)setAllowTakePhotoInLibrary:(BOOL)allowTakePhotoInLibrary {
    _allowTakePhotoInLibrary = allowTakePhotoInLibrary;
    self.actionSheet.configuration.allowTakePhotoInLibrary = allowTakePhotoInLibrary;
}

-(void)setBottomBtnsNormalTitleColor:(UIColor *)bottomBtnsNormalTitleColor {
    _bottomBtnsNormalTitleColor = bottomBtnsNormalTitleColor;
    self.actionSheet.configuration.bottomBtnsNormalTitleColor = bottomBtnsNormalTitleColor;
}

- (void)setMaxSelectCount:(NSInteger)maxSelectCount{
    _maxSelectCount = maxSelectCount;
    self.actionSheet.configuration.maxSelectCount = maxSelectCount;
}

- (void)setAllowSelectVideo:(BOOL)allowSelectVideo{
    _allowSelectVideo = allowSelectVideo;
    self.actionSheet.configuration.allowSelectVideo = allowSelectVideo;
}

- (void)setAllowSelectImage:(BOOL)allowSelectImage {
    _allowSelectImage = allowSelectImage;
    self.actionSheet.configuration.allowSelectImage = allowSelectImage;
}

- (void)setAllowEditVideo:(BOOL)allowEditVideo{
    _allowEditVideo = allowEditVideo;
    self.actionSheet.configuration.allowEditVideo = allowEditVideo;
}

- (void)setUseSystemCamera:(BOOL)useSystemCamera{
    _useSystemCamera = useSystemCamera;
    self.actionSheet.configuration.useSystemCamera = useSystemCamera;
}

- (void)setAllowRecordVideo:(BOOL)allowRecordVideo{
    _allowRecordVideo = allowRecordVideo;
    self.actionSheet.configuration.allowRecordVideo = allowRecordVideo;
}

- (void)setShowSelectedIndex:(BOOL)showSelectedIndex{
    _showSelectedIndex = showSelectedIndex;
    self.actionSheet.configuration.showSelectedIndex = showSelectedIndex;
}

- (void)setMinVideoSelectCountInMix:(NSInteger)minVideoSelectCountInMix{
    _minVideoSelectCountInMix = minVideoSelectCountInMix;
    self.actionSheet.configuration.minVideoSelectCountInMix = minVideoSelectCountInMix;
}

- (void)setMaxVideoSelectCountInMix:(NSInteger)maxVideoSelectCountInMix{
    _maxVideoSelectCountInMix = maxVideoSelectCountInMix;
    self.actionSheet.configuration.maxVideoSelectCountInMix = maxVideoSelectCountInMix;
}

- (void)setShouldAnialysisAsset:(BOOL)shouldAnialysisAsset{
    _shouldAnialysisAsset = shouldAnialysisAsset;
    self.actionSheet.configuration.shouldAnialysisAsset = shouldAnialysisAsset;
}

- (void)setAllowEditImage:(BOOL)allowEditImage{
    _allowEditImage = allowEditImage;
    self.actionSheet.configuration.allowEditImage = allowEditImage;
}

- (void)setSaveNewImageAfterEdit:(BOOL)saveNewImageAfterEdit{
    _saveNewImageAfterEdit = saveNewImageAfterEdit;
    self.actionSheet.configuration.saveNewImageAfterEdit = _saveNewImageAfterEdit;
}

- (void)setIndexLabelBgColor:(UIColor *)indexLabelBgColor {
    _indexLabelBgColor = indexLabelBgColor;
    self.actionSheet.configuration.indexLabelBgColor = indexLabelBgColor;
}

- (void)setMaxRecordDuration:(NSInteger)maxRecordDuration {
    _maxRecordDuration = maxRecordDuration;
    self.actionSheet.configuration.maxRecordDuration = maxRecordDuration;
}

- (void)setAllowTakePhoto:(BOOL)allowTakePhoto {
    _allowTakePhoto = allowTakePhoto;
}

- (DLPhotoActionSheet *)actionSheet{
    if (!_actionSheet) {
        _actionSheet = [[DLPhotoActionSheet alloc] init];
    }
    return _actionSheet;
}

@end
